package com.sb.hot.thread.Main;

import java.util.concurrent.Callable;

/**
 * @author liuxiongkang
 */
public class SafeThread extends Thread {

    private Callable<?> callable;

    private Object result;

    SafeThread() {
        setDaemon(false);
    }

    void call(Callable<?> callable) {
        this.callable = callable;
        start();
    }

    @SuppressWarnings("unchecked")
    <V> V callAndWait(Callable<V> callable) {
        this.callable = callable;
        start();
        try {
            join();
            return (V) this.result;
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
            throw new IllegalStateException(ex);
        }
    }

    @Override
    public void run() {
        // We are safe to refresh the ActionThread (and indirectly call AccessController.getContext()) since our stack doesn't include the RestartClassLoader
        try {
            //Restarter.this.leakSafeThreads.put(new TestThread());
            this.result = this.callable.call();
        } catch (Exception ex) {
            ex.printStackTrace();
            System.exit(1);
        }
    }

}
